#include "../include/e_textures.h"

e_textures::e_textures()
{
    //ctor
}

e_textures::e_textures(const char * filename)
{
    SDL_Surface *surface;
    GLenum texture_format;
    GLint  nOfColors;

        if ( (surface = IMG_Load(filename)) ) {

        if ( (surface->w & (surface->w - 1)) != 0 ) {
            cerr << "warning width is not a power of 2:" << filename << endl;
        }

        if ( (surface->h & (surface->h - 1)) != 0 ) {
            cerr << "warning height is not a power of 2\n" << filename << endl;
        }

        nOfColors = surface->format->BytesPerPixel;
        if (nOfColors == 4) {
                if (surface->format->Rmask == 0x000000ff)
                        texture_format = GL_RGBA;
                else
                        texture_format = GL_BGRA;
        } else if (nOfColors == 3) {
                if (surface->format->Rmask == 0x000000ff)
                        texture_format = GL_RGB;
                else
                        texture_format = GL_BGR;
        } else {
            cerr << "warning: the image is not truecolor..  this will probably break: " << filename << endl;
            //handle beeter
        }

        glGenTextures( 1, &texture );

        glBindTexture( GL_TEXTURE_2D, texture );

        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );


        glTexImage2D( GL_TEXTURE_2D, 0, nOfColors, surface->w, surface->h, 0, texture_format, GL_UNSIGNED_BYTE, surface->pixels );

    } else {
        cerr << "Error loading image" << filename << endl;
    }

    // Free SDL_Surface
    if ( surface ) {
        SDL_FreeSurface( surface );
    }
}

e_textures::~e_textures()
{
    glDeleteTextures(1, &texture);
}

//does this even do anything ?
void e_textures::activate()
{
    glBindTexture( GL_TEXTURE_2D, texture );
}
